package com.fs.iot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fs.common.base.service.AbstractBaseEntityService;
import com.fs.iot.bean.dto.AlarmDTO;
import com.fs.iot.bean.query.AlarmQuery;
import com.fs.iot.domain.entity.AlarmEntity;
import com.fs.iot.domain.mapper.AlarmMapper;
import com.fs.iot.service.AlarmEntityService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 报警服务实现 
 * @author Coder Generator 2025-03-24 17:08:23 
 **/
@Service
public class AlarmEntityServiceImpl extends AbstractBaseEntityService<AlarmEntity, AlarmDTO, AlarmMapper, AlarmQuery> implements AlarmEntityService {
    @Override
    protected QueryWrapper<AlarmEntity> queryToWrapper(AlarmQuery query) {
        return this.createQueryWrapper()
                .eq(StringUtils.isNotBlank(query.getId()), "id", query.getId())
                .in(null != query.getIds(), "id", query.getIds())
                .eq(StringUtils.isNotBlank(query.getDeviceCode()), "device_code", query.getDeviceCode())
                .eq(StringUtils.isNotBlank(query.getCategoryId()), "category_id", query.getCategoryId())
                .eq(null != query.getClosed(), "closed", query.getClosed())
                .orderByDesc("last_up_time");
    }

    /**
     * 增加报警次数
     *
     * @param id 报警记录id
     */
    @Override
    public void addAlarmCount(String id) {
        baseMapper.addAlarmCount(id);
    }

    /**
     * 关闭设备报警
     *
     * @param deviceId  设备id
     * @param defineIds 报警定义id
     */
    @Override
    public void closeAlarms(String deviceId, List<String> defineIds) {
        if (CollectionUtils.isEmpty(defineIds)) {
            return;
        }
        baseMapper.closeAlarms(deviceId, defineIds);
    }
}